const fs = require('fs');
const spark = require('./src/Spark');
const QQBot = require('./src/qqbot');
const Genshin = require('./src/genshin');
const NeDB = require('nedb');
// const EventEmitter = require('events').EventEmitter;
// const Event = new EventEmitter(); 万一呢

let SparkConfig, QQBotConfig, guildID, channelID;

async function readAPI() {
  try{
    let {SparkConfig: sparkcfg, QQBotConfig: qqbotcfg, guildID: gid, channelID: cid} 
      = JSON.parse(fs.readFileSync('./res/JSON/APIconfig.json'));
    SparkConfig = sparkcfg;
    QQBotConfig = qqbotcfg;
    guildID = gid;
    channelID = cid;
    console.log('[INFO]   APIconfig.json 读取成功');
  } catch(err){
    console.log(err);

  };
};// 读取APIconfig.json文件，获取SparkConfig, QQBotConfig, guildID, channelID

async function main(){
  try {
    await readAPI();
    const dbContext = new NeDB({
      filename: './res/DataBase/context.db',
      autoload: true
    });// 实例化context数据库
    const dbMember = new NeDB({
      filename: './res/DataBase/user.db',
      autoload: true
    });// 实例化context数据库
    // const genshin = new Genshin(dbMember);
    const qqbot = new QQBot(QQBotConfig, dbContext, dbMember);
    // const client = qqbot.client;// 创建 client
    const wsqqbot = qqbot.ws;// 创建websocket连接


    qqbot.Init(guildID);


    wsqqbot.on('GUILD_MESSAGES', data => {
      if(data.msg.content === '/test'){
        console.log('测试避让已开启，期间不会做出响应。');
        qqbot.enable = !qqbot.enable;
      }
      qqbot.Reply(data, SparkConfig);// client,

    });
    wsqqbot.on('GUILD_MEMBERS', ()=>{
      qqbot.Init(guildID);
    });// 频道成员发生变化


  } catch(err) {
    console.error(err);
  }
};



main();